      !<@file
      !>@brief Subrotina para a principal entrada de dados.
      !>
      !> Por meio desta rotina determina-se se será resolvido um problema de simulação ou inversão. Todas os dados necessários para solução do problema direto são inseridos por meio desta rotina. 
      subroutine entrada(simula)

      use globais
      implicit none

      integer :: simula!< indica se rodará uma simulação ou uma inversão (simula=0)
      integer :: i,j

      integer:: ierr


      open(1,file= 'dados.txt')

      read(1,*) simula
      read(1,*) delta 
      read(1,*) Lx0 ,Lz0
      read(1,*) Npml 
      read(1,*) Cpml
      read(1,*) rhocte
      read(1,*) fpico,tp,ampS
      read(1,*) tf,ntps
      read(1,*) escFreq , Nomega!escolhe as freqs?

      nttotal = tf*ntps
      fcorte = fpico*(3.*sqrt(Pi))

      write(*,*) '#   simula = ',simula
      write(*,*) '#   delta = ',delta
      write(*,*) '#   Lx0 , Lz0= ', Lx0 , Lz0
      write(*,*) '#   Npml=',npml
      write(*,*) '#   cpml=',cpml
      write(*,*) '#   densidade no meio em kg/m3 = ',rhocte
      write(*,*) '#   freq de pico em Hz=', fpico
      write(*,*) '#   freq de corte em Hz=', fcorte
      write(*,*) '#   tempo de disparo da fonte em s=',tp
      write(*,*) '#   escala na fonte = ', ampS
      write(*,*) '#   tempo de observacao em s = ',tf
      write(*,*) '#   taxa de amostragem = ',ntps
      write(*,*) '#   nttotal = ',nttotal  

      if(escFreq.eq.0)then
	  write(*,*)'alocando omegas...'
	  allocate(omegas(2*Nomega),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif
	  read(1,*)  !pula a linha das freqs
	  do i=1,Nomega
	      omegas(i) = dfloat(i)*2.0d0*Pi/tf ! tempo final de observação
	  enddo
	  do i=1,Nomega
	      omegas(2*Nomega-i+1)=-omegas(i)
	  enddo
      elseif(escFreq.eq.1)then
	  write(*,*)'alocando omegas...'
	  allocate(omegas(2*Nomega),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif
	  read(1,*) (omegas(i),i=1,Nomega) !na verdade aqui leio em Hz
	  do i=1,Nomega
	      omegas(i)=2.0d0*Pi*omegas(i)! agora sim converto para freq angular
	  enddo
	  do i=1,Nomega
	      omegas(2*Nomega-i+1)=-omegas(i)
	  enddo
      endif


      if(escfreq.eq.1)then
	  write(*,*) '#   ',Nomega,'freqs fornecidas Hz:',(omegas(kw)/2.0d0/Pi,kw=1,Nomega)
      elseif(escfreq.eq.0)then
	  write(*,*) '#   ',Nomega,'freqs calculadas automaticamente Hz:'
      endif



      read(1,*)gruposSR !numero de grupos fonte receptor
      write(*,*) '#   numero de grupos fonte receptor:',gruposSR


	write(*,*)'alocando Nrec...'
	allocate(Nrec(gruposSR),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando Nsource...'
	allocate(Nsource(gruposSR),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif



      do igsr= 1,gruposSR
	  write(*,*) '#   grupo fonte receptor numero:',igsr, 'de',gruposSR

	  !dados dos receptores
	  read(1,*)Nrec(igsr)
	  if(Nrec(igsr) .gt. maxNrec)then
	      write(*,*)'Nrec .gt. maxNrec',Nrec,maxNrec
	      stop
	  endif
	  if(Nrec(igsr) .gt. Nrec(1))then
	      write(*,*)'Nrec .gt. Nrec(1)',Nrec,Nrec(1)
	      stop
	  endif

	  write(*,*)'alocando xreceiv...'
	  allocate(xreceiv(Nrec(igsr)),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif

	  write(*,*)'alocando zreceiv...'
	  allocate(zreceiv(Nrec(igsr)),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif

	  do i=1,Nrec(igsr)
	      read(1,*)xreceiv(i),zreceiv(i)
	  enddo
	  write(*,*)'#   Nreceptores lidos',Nrec(igsr)


	  if(igsr.eq.1)then
		write(*,*)'alocando ireceiv...'
		allocate(ireceiv(Nrec(1),gruposSR),stat=ierr)
		if(ierr.eq.0)then
		    write(*,*)'.........Ok'
		else
		    write(*,*)'.........allocation error',ierr     
		    stop
		endif

		write(*,*)'alocando jreceiv...'
		allocate(jreceiv(Nrec(1),gruposSR),stat=ierr)
		if(ierr.eq.0)then
		    write(*,*)'.........Ok'
		else
		    write(*,*)'.........allocation error',ierr     
		    stop
		endif

	  endif

	  do i=1,nrec(igsr)
	      ireceiv(i,igsr) = xreceiv(i)/delta + Npml
	      jreceiv(i,igsr) = zreceiv(i)/delta +1
	      !  write(*,*)xreceiv(i),zreceiv(i),ireceiv(i),jreceiv(i)
	  enddo

	  deallocate(xreceiv)
	  deallocate(zreceiv)










	  !     dados das fontes
	  read(1,*)  Nsource(igsr)
	  if(Nsource(igsr) .gt. maxNsource)then
	      write(*,*)'Nsource .gt. maxNsource',Nsource(igsr),maxNsource
	      stop
	  endif
	  if(Nsource(igsr) .gt. Nsource(1))then
	      write(*,*)'Nsource .gt. Nsource(1)',Nsource(igsr),Nsource(1)
	      stop
	  endif

	  write(*,*)'alocando xsource...'
	  allocate(xsource(Nsource(igsr)),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif

	  write(*,*)'alocando zsource...'
	  allocate(zsource(Nsource(igsr)),stat=ierr)
	  if(ierr.eq.0)then
	      write(*,*)'.........Ok'
	  else
	      write(*,*)'.........allocation error',ierr     
	      stop
	  endif

	  do i=1,Nsource(igsr)
	      read(1,*)xsource(i),zsource(i)
	  enddo
	  write(*,*)'#   Nsource lidos',Nsource(igsr)

	  
	  if(igsr.eq.1)then
		write(*,*)'alocando isource...'
		allocate(isource(Nsource(1),gruposSR),stat=ierr)
		if(ierr.eq.0)then
		    write(*,*)'.........Ok'
		else
		    write(*,*)'.........allocation error',ierr     
		    stop
		endif

		write(*,*)'alocando jsource...'
		allocate(jsource(Nsource(1),gruposSR),stat=ierr)
		if(ierr.eq.0)then
		    write(*,*)'.........Ok'
		else
		    write(*,*)'.........allocation error',ierr     
		    stop
		endif
	  endif

	  do i=1,nsource(igsr)
	      isource(i,igsr) = xsource(i)/delta + Npml
	      jsource(i,igsr) = zsource(i)/delta +1
    ! 	  write(*,*)xsource(i),zsource(i),isource(i),jsource(i)
	  enddo

	  deallocate(xsource)
	  deallocate(zsource)

      enddo ! fim do loop dos grupos de pares fonte receptor


      close(1)

      if(simula.eq.0)then !inversao
	  call system('/bin/bash -O extglob -c '//achar(39)//&
		      &'rm !(dados.txt|parametros.txt|sismD.tgz|veloc.bin|densi.bin|velo0.bin|velo0.ps)'&
		      &//achar(39))! limpa o diretorio de arquivos de saida já existentes
      else!simulaçao
	  call system('/bin/bash -O extglob -c '//achar(39)//'rm !(dados.txt|veloc.bin|densi.bin)'//achar(39))! limpa o diretorio de arquivos de saida já existentes
      endif


      Lx = (aint(Lx0/delta) + 2*(Npml-1))*delta ! a partir daqui, Lx e Lz sao as dimensoes do grid inteiro, com as camadas
      Lz = (aint(Lz0/delta) + 1*(Npml-1))*delta ! uma camada de absorcao + superficie livre


      Nx = aint(Lx/delta)+1
      Nz = aint(Lz/delta)+1

      write(*,*) '#   Nx , Nz= ', Nx , Nz
      write(*,*) '#   Lx , Lz= ', Lx , Lz




	Neq = nx*nz
	nnz=5*Neq
	write(*,*)'alocando f...'
	allocate(f(Neq,Nsource(1)),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando rowind...'
	allocate(rowind(nnz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando colptr...'
	allocate(colptr(neq+1),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando values...'
	allocate(values(nnz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif


	write(*,*)'alocando csix...'
	allocate(csix(Nx),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando csiz...'
	allocate(csiz(Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando c...'
	allocate(c(Nx,Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando rho...'
	allocate(rho(Nx,Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando b...'
	allocate(b(Nx,Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando K...'
	allocate(K(Nx,Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif


	write(*,*)'alocando Preceiv...'
	allocate(Preceiv(Nrec(1),Nsource(1),Nomega,gruposSR),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	write(*,*)'alocando creal4...'
	allocate(creal4(Nx,Nz),stat=ierr)
	if(ierr.eq.0)then
	    write(*,*)'.........Ok'
	else
	    write(*,*)'.........allocation error',ierr     
	    stop
	endif

	medidas=0
	do igsr = 1, gruposSR
	    medidas = medidas + Nrec(igsr)*Nsource(igsr)
	enddo

      return

      end subroutine entrada